<?php
/**
 * 客户跟进记录
 */
namespace app\common\model;

use basic\ModelBasic;

class CustomerNotice extends ModelBasic
{
	use \traits\ModelTrait;


	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['uid']) && $where['uid'] > 0 ) {
            $model->where(sprintf('%s.uid = %d' , $alias, $where['uid']));
        }

        if ( isset($where['sales_admin_id']) && $where['sales_admin_id'] > 0 ) {
            $model->where(sprintf('%s.sales_admin_id = %d' , $alias, $where['sales_admin_id']));
        }

        if ( isset($where['notice_date']) && $where['notice_date'] > 0 ) {
            $model->where(sprintf("%s.notice_date = '%s'" , $alias, $where['notice_date']));
        }

        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
    	$count = self::setWhere($where)->count();
    	$data  = [];
    	if ( $count == 0 ) {
    		return compact('data','count');
    	}

        $obj = self::setWhere($where,'m');
		
		$data = $obj->field("m.id,m.cus_uid,m.sales_admin_id,m.uid,m.notice_date,m.notice_time,m.notice_content,m.process_time")
		->order('m.notice_date desc,m.notice_time desc')
		->page($where['page'] ?? 1, $where['limit'] ?? 15)->select();
		
        if ( !empty($data) ) {
        	$data = $data->toArray();
        	$data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 处理列表数据
     * @Author   FW
     * @DateTime 2021-01-12
     * @Email    123763712@qq.com
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$value) {
        	$value['process_time_display'] = '未处理';
        	if ( $value['process_time'] > 0 ) {
            	$value['process_time_display'] = date('Y年m月d日 H:i:s' , $value['process_time']);
            }
        }
        return $list;
    }
}//Class End
